package com.gxa.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.FirmCustomerMapper;
import com.gxa.modules.pojo.Enterprise_Customer;
import com.gxa.modules.service.FirmCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class FirmCustomerServiceImpl extends ServiceImpl<FirmCustomerMapper, Enterprise_Customer> implements FirmCustomerService {

    @Autowired
    private FirmCustomerMapper firmCustomerMapper;

    @Override
    public JsonResult findFirmCustomerByEnterpriseName(Long currentPage, Long pageSize, String enterprise_name) {
        JsonResult jr=new JsonResult();
        try{
            Long start =(currentPage-1)*pageSize;
            Long off = pageSize;
            Page<Enterprise_Customer> page = new Page<>(start,off);
            QueryWrapper<Enterprise_Customer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq((StringUtils.isNotBlank(enterprise_name)),"enterprise_name",enterprise_name);
            Page<Enterprise_Customer> enterprise_customerPage=firmCustomerMapper.selectPage(page,queryWrapper);
            jr.setCount((int) enterprise_customerPage.getTotal());
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setData(enterprise_customerPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络开小差");
        }
        return jr;
    }

    @Override
    public R delFirmCustomerById(Integer id) {
        R r = new R();
        try{
            int result = firmCustomerMapper.deleteById(id);
            if (result>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
        }catch (Exception e){
                r.setCode(500);
                r.setMsg("网络开小差");
        }
        return r;
    }

    @Override
    public R addFirmCustomer(Enterprise_Customer enterprise_customer) {
        R r =new R();
        try{
            Date date = new Date();
            SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
            String datetime = format.format(date);
            enterprise_customer.setCreationTime(datetime);
            int result = firmCustomerMapper.insert(enterprise_customer);
            if (result>0){
               r.setCode(200);
               r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }

    @Override
    public R updateFirmCustomer(Enterprise_Customer enterprise_customer) {
        R r = new R();
        try{
            int result = firmCustomerMapper.updateById(enterprise_customer);
            if (result>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }

        return r;
    }

    @Override
    public R queryFirmCustomerById(Integer id) {
        R r =new R();
        try{
            Enterprise_Customer enterprise_customer = firmCustomerMapper.selectById(id);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(enterprise_customer);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络开小差");
        }
        return r;
    }
}
